package Interface.Table;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Controleur.ControleurGestionTable;
import Interface.InterfaceListeNomTable;
import Interface.InterfaceObservable;
import Interface.InterfaceObservateur;
import Interface.Observateur;
import Metier.Attribut;

public class InterfaceGestionTable extends JInternalFrame implements ActionListener,
WindowListener,InterfaceObservateur, Observateur{

	private List<InterfaceObservable> listeObservable= new ArrayList<InterfaceObservable>();
	private ControleurGestionTable controleur;
	private JPanel contentPaneDroit;
	private JPanel contentPaneGauche;
	private JPanel contentAttr;

	private JButton btAjouterTable,btAjouterAttribut,btSupprimerTable;
	private JSplitPane split;
	JScrollPane attrscrollPane ;
	private AttributPanel panelTitle = new AttributPanel();
	private List<AttributPanel> listPanelAttribut = new ArrayList<AttributPanel>();
	private List<JButton> listButtonSup = new ArrayList<JButton>();
	private List<JButton> listButtonModif = new ArrayList<JButton>();

	private JMenuBar menuBar = new JMenuBar();
	private JMenu menuTable = new JMenu("Table"),
	menuAttribut = new JMenu("Attribut");
	private int indexList=0;

	private JMenuItem menuItemCreateTable = new JMenuItem("Ajouter"),
	menuItemDeleteTable = new JMenuItem("Supprimer"),
	menuItemCreateAttribut = new JMenuItem("Ajouter"),
	menuItemDeleteAttribut = new JMenuItem("Supprimer"),
	menuItemUpdateAttribut = new JMenuItem("Modifier");

	private InterfaceListeNomTable interfaceListeTable;
	
	public InterfaceGestionTable() {

		this.controleur = ControleurGestionTable.getInstance();
		this.setName("Gestion des tables");
		setTitle("Gestion des tables");
		setSize(1200,510);
		this.setClosable(true);
		menuItemCreateTable.addActionListener(this);
		menuItemDeleteTable.addActionListener(this);
		menuItemCreateAttribut.addActionListener(this);
		menuItemDeleteAttribut.addActionListener(this);
		menuItemUpdateAttribut.addActionListener(this);

		menuTable.add(menuItemCreateTable);
		menuTable.add(menuItemDeleteTable);

		menuAttribut.add(menuItemCreateAttribut);
		menuAttribut.add(menuItemDeleteAttribut);
		menuAttribut.add(menuItemUpdateAttribut);


		menuBar.add(menuTable);
		menuBar.add(menuAttribut);


		this.setJMenuBar(menuBar);
		menuAttribut.setEnabled(false);

		/*	Panneau Gauche */
		contentPaneGauche = new JPanel();
		this.contentPaneGauche.setLayout(new BorderLayout());

		/*	Panneau Droit */
		contentPaneDroit = new JPanel();
		this.contentPaneDroit.setLayout(new BorderLayout());


		/*	Bouton AjouterTable	*/
		btAjouterTable = new JButton("Ajouter une table");
		btAjouterTable.addActionListener(this);
		this.contentPaneGauche.add(btAjouterTable,BorderLayout.SOUTH);

		this.setLayout(new BorderLayout());

		/*	Bouton SupprimerTable	*/
		btSupprimerTable = new JButton("Supprimer la table");
		btSupprimerTable.addActionListener(this);
		btSupprimerTable.setEnabled(false);

		/*	Bouton AjouterAttribut	*/
		btAjouterAttribut = new JButton("Ajouter une Attribut");
		btAjouterAttribut.addActionListener(this);
		btAjouterAttribut.setEnabled(false);

		/*	Scroll Bare de la partie gauche	*/
		interfaceListeTable = new InterfaceListeNomTable();
		interfaceListeTable.addObservateur(this);
		this.contentPaneGauche.add(interfaceListeTable);

		/*	Panneau Attribut	*/
		contentAttr = new JPanel();
		//this.contentPaneDroit.add(contentAttr,BorderLayout.CENTER);

		/*	Scroll Bare de la partie droite	*/
		attrscrollPane = new JScrollPane(contentAttr);
		attrscrollPane.setPreferredSize(new Dimension(450,500));
		this.contentPaneDroit.add(attrscrollPane);

		/*	Panneau Bouton	*/
		JPanel paneau = new JPanel();
		paneau.setLayout(new GridLayout(1,2));
		paneau.add(btSupprimerTable);
		paneau.add(btAjouterAttribut);
		this.contentPaneDroit.add(paneau,BorderLayout.SOUTH);

		/*	Split	Entre la parti gauche et droite	*/

		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.contentPaneGauche, this.contentPaneDroit);
		this.add(split, BorderLayout.CENTER);

		/*	Ajout d'écouteur sur la fenetre	*/
		this.setVisible(true);
	}

	public void affichageAttribut(String table) {
		menuAttribut.setEnabled(true);
		this.listPanelAttribut.clear();
		this.contentAttr.removeAll();
		if (this.interfaceListeTable.getValeur() != null)
		{
			List<Attribut> list = controleur.getListeAttribut(table);
			if (list.size()<10)
				this.contentAttr.setLayout(new GridLayout(10,0));
			else
				this.contentAttr.setLayout(new GridLayout(list.size()+1,0));
			this.contentAttr.add(this.panelTitle);
			this.listButtonSup.clear();
			this.listButtonModif.clear();
			for (int i = 0; i < list.size(); i++) {
				this.listPanelAttribut.add(new AttributPanel(list.get(i)));
				this.listButtonSup.add(new JButton("Sup"));
				this.listButtonSup.get(i).setPreferredSize(new Dimension(60,20));
				this.listButtonSup.get(i).addActionListener(this);
				this.listPanelAttribut.get(i).add(this.listButtonSup.get(i));
				this.listButtonModif.add(new JButton("Modif"));
				this.listButtonModif.get(i).setPreferredSize(new Dimension(100,20));
				this.listButtonModif.get(i).addActionListener(this);
				this.listPanelAttribut.get(i).add(this.listButtonModif.get(i));
				this.contentAttr.add(this.listPanelAttribut.get(i));
			}
		}
		this.setVisible(true);
		this.repaint();
		this.revalidate();
	}

	public void windowActivated(WindowEvent e) {
	}


	public void windowClosed(WindowEvent e) {
		if (e.getWindow().getName().equals("InterfaceAttribut"))
			this.affichageAttribut(this.interfaceListeTable.getValeur());
		if (e.getWindow().getName().equals("Interface1Attribut"))
			list();
	}

	public void windowClosing(WindowEvent e) {
		if (e.getWindow().getName().equals("Gestion des tables"))
			this.dispose();
	}

	public void windowDeactivated(WindowEvent e) {

	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {

	}

	public void actionPerformed(ActionEvent e) {

		Object action = e.getSource();

		if (action instanceof JButton)
		{
			int nbSup = this.nbJbtSup((JButton) action);
			int nbModif = this.nbJbtModif((JButton) action);
			if (nbSup != -1)
			{
				if(this.controleur.deleteAttribut(this.listPanelAttribut.get(nbSup).getAttribut()))
				{
					this.affichageAttribut(this.interfaceListeTable.getValeur());
					this.updateTable(controleur.getTable());	
				}
			}
			else if (nbModif != -1)
			{
				if (!InterfaceAjouterAttribut.getEtat())
				{
					InterfaceAjouterAttribut iAA= new InterfaceAjouterAttribut(controleur.getAttribut(this.listPanelAttribut.get(nbModif).getAttribut()),this.listeObservable);
					iAA.addWindowListener(this);
				}
			}
		}
		if (action == this.btAjouterTable || action == this.menuItemCreateTable)
		{
			if (!InterfaceAjouterAttribut.getEtat())
			{
				InterfaceAjouterAttribut iAA = new InterfaceAjouterAttribut(this.listeObservable);
				iAA.addWindowListener(this);
			}
		}
		if ((action == this.btAjouterAttribut || action == this.menuItemCreateAttribut)&& this.interfaceListeTable.getValeur()!=null)
		{
			if (!InterfaceAjouterAttribut.getEtat())
			{
				InterfaceAjouterAttribut iAA= new InterfaceAjouterAttribut(this.interfaceListeTable.getValeur(),this.listeObservable);
				iAA.addWindowListener(this);
			}
		}
		if (action == this.btSupprimerTable)
		{
			if(this.controleur.deleteTable(this.interfaceListeTable.getValeur()))
			{
				this.deleteTable(this.interfaceListeTable.getValeur());
				list();
			}
		}

		if (action == this.menuItemDeleteTable)
		{
			JOptionPane jop = new JOptionPane(), jop2 = new JOptionPane();
			String nom = (String)jop.showInputDialog(null,"Veuillez choisir la table a supprimer !",
					"Suppression de table !",
					JOptionPane.QUESTION_MESSAGE,null,this.controleur.getListeNomTable(),null);
			if (nom!=null)
			{
				if (this.controleur.deleteTable(nom))
				{
					this.deleteTable(this.interfaceListeTable.getValeur());
					this.list();
				}
			}
		}

		if (action == this.menuItemDeleteAttribut)
		{
			JOptionPane jop = new JOptionPane(), jop2 = new JOptionPane();
			String nom = (String)jop.showInputDialog(null,"Veuillez choisir l'attribut a supprimer !",
					"Suppression d'un attribut  de :"+this.controleur.getTable()+" !",
					JOptionPane.QUESTION_MESSAGE,null,this.controleur.getListNomAttribut(),null);
			if (nom!=null)
			{
				if (!InterfaceAjouterAttribut.getEtat())
				{
					if(this.controleur.deleteAttribut(this.controleur.getUnAttribut(nom)))
					{					
						this.affichageAttribut(this.interfaceListeTable.getValeur());
						this.updateTable(controleur.getTable());
					}
				}
			}
		}

		if (action == this.menuItemUpdateAttribut)
		{
			JOptionPane jop = new JOptionPane(), jop2 = new JOptionPane();
			String nom = (String)jop.showInputDialog(null,"Veuillez choisir la table a supprimer !",
					"Modification d'un attribut  de :"+this.controleur.getTable()+" !",
					JOptionPane.QUESTION_MESSAGE,null,this.controleur.getListNomAttribut(),null);
			if (nom!=null)
			{
				if (!InterfaceAjouterAttribut.getEtat())
				{
					InterfaceAjouterAttribut iAA= new InterfaceAjouterAttribut(this.controleur.getUnAttribut(nom),this.listeObservable);
					iAA.addWindowListener(this);
				}
			}
		}
	}


	public void valueChanged(ListSelectionEvent listEvent) {

		if(!listEvent.getValueIsAdjusting())
		{
			btSupprimerTable.setEnabled(true);
			btAjouterAttribut.setEnabled(true);
			this.affichageAttribut(this.interfaceListeTable.getValeur());
			indexList = this.interfaceListeTable.getValeurIndex();
		}
	}

	public void list()
	{
		this.interfaceListeTable.recharger();
		menuAttribut.setEnabled(false);
		btSupprimerTable.setEnabled(false);
		this.btAjouterAttribut.setEnabled(false);
	}

	public int nbJbtSup(JButton bt)
	{
		for(int i=0;i<this.listButtonSup.size();i++)
		{
			if (this.listButtonSup.get(i).equals(bt))
			{
				return i;
			}
		}
		return -1;
	}
	public int nbJbtModif(JButton bt)
	{
		for(int i=0;i<this.listButtonModif.size();i++)
		{
			if (this.listButtonModif.get(i).equals(bt))
			{
				return i;
			}
		}
		return -1;
	}

	public void addTable(String nomTable) {
		for(int i=0;i<this.listeObservable.size();i++)
		{
			this.listeObservable.get(i).addTable(nomTable);
		}
	}

	public void deleteTable(String nomTable) {
		for(int i=0;i<this.listeObservable.size();i++)
		{
			this.listeObservable.get(i).deleteTable(nomTable);
		}
	}

	public void updateTable(String nomTable) {
		for(int i=0;i<this.listeObservable.size();i++)
		{
			this.listeObservable.get(i).updateTable(nomTable);
		}
	}

	public void addObservateur(InterfaceObservable observable) {
		this.listeObservable.add(observable);
	}

	public void active() {
		btSupprimerTable.setEnabled(true);
		btAjouterAttribut.setEnabled(true);
		this.affichageAttribut(this.interfaceListeTable.getValeur());
		indexList = this.interfaceListeTable.getValeurIndex();
	}
}